<?php 
namespace Home\Controller ; 
class CartController extends HomeController {
    public $cartLogic; // 购物车逻辑操作类
    public $user_id = 0;
    public $user = array();    
     /**
     * 初始化函数
     */ 
    public function _initialize() {       
        parent::_initialize();
        $this->cartLogic = new \Home\Logic\CartLogic();
        $user = session('user_auth');
        $this->session_id = session_id() ; 
        if($user)
        {
        	$this->user = $user;
        	$this->user_id = $user['uid'];
        	$this->assign('user',$user); //存储用户信息        
        }               
    }

	function index(){
		$this->display();
	}

	  /*
     * ajax 请求获取购物车列表
     */
    public function ajaxCartList()
    {    
        $post_goods_num = I("goods_num"); // goods_num 购物车商品数量
        $post_cart_select = I("cart_select"); // 购物车选中状态                               
        $where = " session_id = '$this->session_id' "; // 默认按照 session_id 查询
        $this->user_id && $where = " user_id = ".$this->user_id; // 如果这个用户已经等了则按照用户id查询
        
        $cartList = M('Cart')->where($where)->getField("id,goods_num,selected"); 
        if($post_goods_num) 
        {
            // 修改购物车数量 和勾选状态
            foreach($post_goods_num as $key => $val)
            {   
                $data['goods_num'] = $val < 1 ? 1 : $val;
                $data['selected'] = $post_cart_select[$key] ? 1 : 0 ;                               
                if(($cartList[$key]['goods_num'] != $data['goods_num']) || ($cartList[$key]['selected'] != $data['selected'])) 
                    M('Cart')->where("id = $key")->save($data);
            }
            $this->assign('select_all', $_POST['select_all']); // 全选框 
        }
                
        $result = $this->cartLogic->cartList($this->user, $this->session_id,1); // 选中的商品        
        if(empty($result['total_price']))
            $result['total_price'] = Array( 'total_fee' =>0, 'cut_fee' =>0, 'num' => 0);
        
        $this->assign('cartList', $result['cartList']); // 购物车的商品                
        $this->assign('total_price', $result['total_price']); // 总计
        $this->assign('anum' , $result ['anum']);
        $this->display('ajax_cart_list');
    }
    

    function ajaxAddCart(){
        $goods_id = I("goods_id"); // 商品id
        $goods_num = I("goods_num");// 商品数量       
        $result = $this->cartLogic->addCart($goods_id, $goods_num,$this->session_id,$this->user_id); // 将商品加入购物车                     
        if($result['status'] >= 1){
            exit(json_encode(array(
                'status' => 1 ,
                'info'   => '增加成功' ,
                'url'    => U('Cart/index') ,
                )));
        }else{
            exit(json_encode(array(
                'stauts' => 0 ,
                'info'  => $result['msg'] , 
                'url' => '',
                )));
        }
    }

    /**
     * ajax 删除购物车的商品
     */
    public function ajaxDelCart()
    {       
        $ids = I("ids"); // 商品 ids        
        $result = M("Cart")->where(" id in ($ids)")->delete(); // 删除id为5的用户数据
        $return_arr = array('status'=>1,'msg'=>'删除成功','result'=>''); // 返回结果状态       
        exit(json_encode($return_arr));
    }
      /**
     * 购物车第二步确定页面
     */
    public function cart2()
    {        
        
        if($this->user_id == 0)
        {
            set_redirect_url(U('Cart/cart2'));
            $this->error('请先登陆',U('Home/User/login'));
        }
            
        
        if($this->cartLogic->cart_count($this->user_id,1) == 0 ) 
            $this->error ('你的购物车没有选中商品','Cart/cart');
        
        $result = $this->cartLogic->cartList($this->user, $this->session_id,1,1); // 获取购物车商品        
        $shippingList = M('Plugin')->where("`type` = 'shipping' and status = 1")->select();// 物流公司                
        
        $Model = new \Think\Model(); // 找出这个用户的优惠券 没过期的  并且 订单金额达到 condition 优惠券指定标准的               
        $sql = "select c1.name,c1.money,c1.condition, c2.* from __PREFIX__coupon as c1 inner join __PREFIX__coupon_list as c2  on c2.cid = c1.id and c1.type in(0,1,2,3) and order_id = 0  where c2.uid = {$this->user_id}  and ".time()." < c1.use_end_time and c1.condition <= {$result['total_price']['total_fee']}";
        $couponList = $Model->query($sql);
               
        $this->assign('couponList', $couponList); // 优惠券列表
        $this->assign('shippingList', $shippingList); // 物流公司
        $this->assign('cartList', $result['cartList']); // 购物车的商品                
        $this->assign('total_price', $result['total_price']); // 总计                               
        $this->display();
    }


}	